package com.um.jdy.common.activity.service.impl;

import cn.hutool.core.date.DateUtil;
import com.um.jdy.common.activity.code.CouponsCode;
import com.um.jdy.common.activity.entity.po.CouponsUser;
import com.um.jdy.common.activity.service.CouponsUserService;
import com.um.jdy.common.shop.enums.CouponsEnum;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.um.springboot.starter.orm.service.BaseEntityServiceImpl;
import com.um.jdy.common.activity.service.CouponsService;
import com.um.jdy.common.activity.dao.CouponsDao;
import com.um.jdy.common.activity.entity.po.Coupons;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务实现类：优惠券表【备份】
 */
@Service
@Transactional
@Slf4j
public class CouponsServiceImpl extends BaseEntityServiceImpl<CouponsDao,Coupons>  implements CouponsService{

    @Autowired
    CouponsUserService couponsUserService;



    /**
     * 领取优惠券
     *
     * @param activityCouponsId
     * @param memberUserId
     * @return
     */
    @Override
    public void receive(String activityCouponsId, String memberUserId) {
        Coupons coupons = getRow(activityCouponsId);
        if(null == coupons){
            throw new ServiceException(CouponsCode.CouponsNotExist);
        }

        CouponsUser couponsUser = couponsUserService.getRow(new String[]{"activity_coupons_id", "member_user_id"},
                new Object[]{activityCouponsId, memberUserId});
        if(null != couponsUser){
            throw new ServiceException(CouponsCode.Received);
        }

        long couponsReceiveCount = couponsUserService.getRows(new String[]{"activity_coupons_id"},new String[]{activityCouponsId}).size();
        if(coupons.getTotalNumType().equals(CouponsEnum.TotalNumType.Limit.name()) && couponsReceiveCount >= coupons.getTotalNum()){
            throw new ServiceException(CouponsCode.ReceivedCompleted);
        }

        couponsUser = new CouponsUser();
        couponsUser.setMemberUserId(memberUserId);
        couponsUser.setActivityCouponsId(activityCouponsId);
        couponsUser.setCreateTime(DateUtil.date());
        couponsUser.setComId(coupons.getComId());
        couponsUserService.saveRow(couponsUser);
    }

    /**
     * 兑换优惠券
     *
     * @param code
     * @param memberUserId
     * @return
     */
    @Override
    public void exchange(String code, String memberUserId) {
        // Map exchange = dao.queryMap(TableName.ACTIVITY_COUPONS_EXCHANGE,"code",code);
        // if(null == exchange || StringUtil.getTrim(exchange.get("is_redeemed")).equals(YesOrNo.YES.getKey())){
        //     throw new ServiceException("80006");
        // }
        //
        // String activityCouponsId = StringUtil.getTrim(exchange.get("activity_coupons_id"));
        // Map coupons = dao.queryMap(TableName.ACTIVITY_COUPONS,activityCouponsId);
        //
        // if(null == coupons || StringUtil.getTrim(coupons.get("status")).equals(YesOrNo.NO.getKey())){
        //     throw new ServiceException("80007");
        // }
        //
        // String now = cn.hutool.core.date.DateUtil.now();
        // String endDate = StringUtil.getTrim(coupons.get("end_time"));
        // if(com.hsh.hshcommon.utils.DateUtil.compareDate(now,endDate,"yyyy-MM-dd HH:mm:ss") > 0){
        //     throw new ServiceException("80008");
        // }
        //
        // exchange.put("is_redeemed",YesOrNo.YES.getKey());
        // exchange.put("app_account_id",userService.getTokenUserId());
        // exchange.put("redeemed_time",now);
        // this.save(TableName.ACTIVITY_COUPONS_EXCHANGE,exchange);
        //
        // receive(activityCouponsId, memberUserId);
    }
}
